package leetcode;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:用队列实现栈
 * User: 东莞呵呵
 * Date:2022-07-15
 * Time:16:59
 */
public class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public int useSize;
    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    public void push(int x) {
        if(!queue1.isEmpty()){
            queue1.offer(x);
        }else if (!queue2.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
        useSize++;
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int curSize=queue1.size()-1;
            for (int i = 0; i < curSize; i++) {
                queue2.offer(queue1.poll());
            }
            useSize--;
            return queue1.poll();

        }else {
            int curSize=queue2.size()-1;
            for (int i = 0; i < curSize; i++) {
                queue1.offer(queue2.poll());
            }
            useSize--;
            return queue2.poll();

        }

    }

    public int top() {
        if(empty()){
            return -1;
        }
        int curSize;
        int ret=0;
        if(!queue1.isEmpty()){
            curSize = queue1.size();
            for (int i = 0; i < curSize; i++) {
                ret=queue1.poll();
                queue2.offer(ret);
            }

        }else {
            curSize = queue2.size();
            for (int i = 0; i < curSize; i++) {
                ret=queue2.poll();
                queue2.offer(ret);
            }

        }
        return ret;
    }

    public boolean empty() {
        return useSize==0;
    }
}
